﻿//-----------------------------------------------------------------------
// <copyright file="GraphToolTipHandler.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.Model
{
    using System.Drawing;
    using System.Windows.Forms;
    using Redmist.UsageMonitor.View;

    /// <summary>Handles the generation of dynamic tooltips for the usage history graphs.</summary>
    public class GraphToolTipHandler
    {
        /// <summary>A Redmist.UsageMonitor.View.IUsageView representing the view containing the usage history graphs.</summary>
        private IUsageView usageView;

        /// <summary>A System.Drawing.Point used for remembering the last registered mouse position.</summary>
        private Point lastMousePosition;

        /// <summary>Initializes a new instance of the GraphToolTipHandler class.</summary>
        /// <param name="usageView">A Redmist.UsageMonitor.View.IUsageView representing the view containing the usage history graphs.</param>
        public GraphToolTipHandler(IUsageView usageView)
        {
            this.usageView = usageView;
            this.usageView.GraphMouseMoved += new MouseEventHandler(this.UsageView_GraphMouseMoved);
        }

        /// <summary>Updates the graph tool tip based on the current mouse X position on the download graph.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void UsageView_GraphMouseMoved(object sender, MouseEventArgs e)
        {
            if (this.usageView.Model != null)
            {
                this.ShowGraphTooltip(this.usageView.GraphMouseX); 
            }
        }

        /// <summary>Sets the tooltips for the view's download and upload graphs.</summary>
        /// <param name="graphMouseX">The X position of the mouse on the graph.</param>
        private void ShowGraphTooltip(int graphMouseX)
        {
            if (Cursor.Position.Equals(this.lastMousePosition))
            {
                return;
            }

            this.lastMousePosition = Cursor.Position;
            int graphDays = this.usageView.GraphHistoryDays;
            float barWidth = (float)this.usageView.HistoryGraphWidth / graphDays;
            int mouseXdays = (int)(graphMouseX / barWidth);
            if (mouseXdays < 0 || mouseXdays >= graphDays)
            {
                return;
            }

            string downloadToolTip = this.usageView.Model.HistoryStartDate.AddDays(mouseXdays).ToString("ddd, dd MMM yyyy");
            string uploadToolTip = downloadToolTip;
            switch (this.usageView.SelectedHistoryMode)
            {
                case HistoryView.Combined:
                    int peak = this.usageView.Model.CurrentDownloadHistory[mouseXdays];
                    int offPeak = this.usageView.Model.SecondaryDownloadHistory[mouseXdays];
                    downloadToolTip += this.CreateCombinedDataString(peak, offPeak);
                    peak = this.usageView.Model.CurrentUploadHistory[mouseXdays];
                    offPeak = this.usageView.Model.SecondaryUploadHistory[mouseXdays];
                    uploadToolTip += this.CreateCombinedDataString(peak, offPeak);
                    break;
                case HistoryView.OffPeak:
                    downloadToolTip += this.CreateSingleDataString(this.usageView.Model.SecondaryDownloadHistory[mouseXdays]);
                    uploadToolTip += this.CreateSingleDataString(this.usageView.Model.SecondaryUploadHistory[mouseXdays]);
                    break;
                case HistoryView.Peak:
                    downloadToolTip += this.CreateSingleDataString(this.usageView.Model.CurrentDownloadHistory[mouseXdays]);
                    uploadToolTip += this.CreateSingleDataString(this.usageView.Model.CurrentUploadHistory[mouseXdays]);
                    break;
                default:
                    break;
            }

            this.usageView.SetDownloadGraphToolTip(downloadToolTip);
            this.usageView.SetUploadGraphToolTip(uploadToolTip);
        }

        /// <summary>Creates the usage string for a graph containing a single set of data.</summary>
        /// <param name="value">The number of megabytes of usage.</param>
        /// <returns>A System.String representing a megabyte value.</returns>
        private string CreateSingleDataString(int value)
        {
            return string.Format("\n{0} MB", value);
        }

        /// <summary>Creates the usage string for a usage graph containing both peak and off peak data.</summary>
        /// <param name="peak">The number of megabytes of peak usage.</param>
        /// <param name="offPeak">The number of megabytes of off peak usage.</param>
        /// <returns>A System.String representing peak and off peak usage information.</returns>
        private string CreateCombinedDataString(int peak, int offPeak)
        {
            return string.Format("\nPeak: {0} MB\nOff Peak: {1} MB\nTotal: {2} MB", peak, offPeak, peak + offPeak);
        }
    }
}
